#include "D:\sky\MPLAB IDE PROJECTS\Standard Projects\Header_xc8.h"

void main()
{
	unsigned char keypad;
	ADCON1 = 0x0F;
	while(1)
	{
		keypad = 17;
		while(keypad>16)
		{
			TRISD = 0x01;
			TRISD = ~TRISD;
			PORTDbits.RD0 = 1;
			if(PORTDbits.RD4 == 1)keypad = 1;
			else if(PORTDbits.RD5 == 1)keypad = 2;
			else if(PORTDbits.RD6 == 1)keypad = 3;
			else if(PORTDbits.RD7 == 1)keypad = 4;
			
			TRISD = 0x02;
			TRISD = ~TRISD;
			PORTDbits.RD1 = 1;
			if(PORTDbits.RD4 == 1)keypad = 5;
			else if(PORTDbits.RD5 == 1)keypad = 6;
			else if(PORTDbits.RD6 == 1)keypad = 7;
			else if(PORTDbits.RD7 == 1)keypad = 8;
						
			TRISD = 0x04;
			TRISD = ~TRISD;
			PORTDbits.RD2 = 1;
			if(PORTDbits.RD4 == 1)keypad = 9;
			else if(PORTDbits.RD5 == 1)keypad = 10;
			else if(PORTDbits.RD6 == 1)keypad = 11;
			else if(PORTDbits.RD7 == 1)keypad = 12;
			
			TRISD = 0x08;
			TRISD = ~TRISD;
			PORTDbits.RD3 = 1;
			if(PORTDbits.RD4 == 1)keypad = 13;
			else if(PORTDbits.RD5 == 1)keypad = 14;
			else if(PORTDbits.RD6 == 1)keypad = 15;
			else if(PORTDbits.RD7 == 1)keypad = 16;
		}
		TRISB = 0x00;
		PORTB = keypad;
		PORTB = 0x00;
	}
	while(1);
}